package com.yhp.dao.impl;

import com.yhp.bean.Menu;
import com.yhp.bean.Role;
import com.yhp.dao.MiddleDao;
import com.yhp.util.Dbutils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MiddleDaoImpl extends Dbutils implements MiddleDao {
    @Override
    public int addMiddle(int id, String[] menuId) {
        int l = 0;
        try {
            int a = menuId.length;
            for (int i=0;i<a;i++){
                String sql = "insert into middle values(null,?,?)";
                List param = new ArrayList();
                param.add(id);
                param.add(menuId[i]);
                int update = update(sql, param);
            }
            l=1;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return l;
    }

    @Override
    public int deleteMiddleRole(int id) {
        int update = 0;
        try {
            String sql = "delete from middle where roleid=?";
            List param = new ArrayList();
            param.add(id);
            update = update(sql, param);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return update;
    }

    @Override
    public int deleteMiddleMenu(int id) {
        int update = 0;
        try {
            String sql = "delete from middle where menuid=?";
            List param = new ArrayList();
            param.add(id);
            update = update(sql, param);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return update;
    }


    @Override
    public List<Menu> findById(int id) {
        List<Menu> list = null;
        try {
            String sql = "SELECT * FROM middle m,role r,menu u where m.roleid=r.roleid and u.menuid=m.menuid and r.roleid=?";
            List param = new ArrayList();
            param.add(id);
            ResultSet query = query(sql, param);
            list = new ArrayList<>();
            while (query.next()){
                Menu menu = new Menu();
                menu.setMenuId(query.getInt("menuid"));
                menu.setMenuName(query.getString("menuname"));
                menu.setUpMenuId(query.getInt("upmenuid"));
                list.add(menu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }

        return list;
    }

    @Override
    public int updateMiddleRole(Role role, String[] menuIds) {
        int l = 0;
        try {
            String sql = "delete from middle where roleid=?";
            List param = new ArrayList();
            param.add(role.getRoleId());
            update(sql,param);
            l = addMiddle(role.getRoleId(), menuIds);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }

        return l;
    }


}
